﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Adapters;

namespace Code5Systems.FormMaster
{
	public class C5RadioButtonList : RadioButtonList
	{
		public C5RadioButtonList()
		{
			this.LabelCssClass = String.Empty;
		}
		public string LabelCssClass { get; set; }
	}

	public class C5RadioButtonListAdapter : WebControlAdapter
	{
		private List<string> reservedAttributes = new List<string>()
		{
			"class",
			"data-type",
			"name",
			"id"
		};

		protected override void RenderBeginTag(HtmlTextWriter writer)
		{
			var adaptedControl = (C5RadioButtonList)Control;
			writer.AddAttribute("class", "C5SLLC_chklistsub");
			writer.AddAttribute("data-type", "radiobuttonlist");
			writer.AddAttribute("name", adaptedControl.ClientID + "div");
			writer.AddAttribute("id", adaptedControl.ClientID);

			foreach (string key in adaptedControl.Attributes.Keys)
			{
				if (!reservedAttributes.Contains(key.ToLower()))
				{
					writer.AddAttribute(key, adaptedControl.Attributes[key]);
				}
			}

			writer.RenderBeginTag("div");
		}

		protected override void RenderEndTag(HtmlTextWriter writer)
		{
			writer.RenderEndTag();
		}

		protected override void EndRender(HtmlTextWriter writer)
		{

			if (Page != null)
			{
				Page.ClientScript.RegisterForEventValidation(Control.UniqueID);
			}
			base.EndRender(writer);
		}

		protected override void RenderContents(HtmlTextWriter writer)
		{
			//base.RenderContents(writer);
			//return;

			var adaptedControl = (C5RadioButtonList)Control;
			int itemCounter = 0;
			string styles = String.Empty;
			string classes = String.Empty;
			string rootId = String.Empty;

			if (!String.IsNullOrWhiteSpace(adaptedControl.Attributes["data-id"]))
			{
				rootId = adaptedControl.Attributes["data-id"];
			}

			writer.Indent++;
			foreach (ListItem item in adaptedControl.Items)
			{
				// item div
				if (adaptedControl.RepeatDirection == RepeatDirection.Horizontal)
				{
					if (itemCounter > 0 && itemCounter % adaptedControl.RepeatColumns == 0)
					{
						styles = String.Format("width:{0}px;", adaptedControl.Width.Value);
						classes = " C5SLLC_chklstitemend";
					}
					else
					{
						styles = String.Format("width:{0}px;", adaptedControl.Width.Value);
						classes = " C5SLLC_chklstitem";
					}
					writer.AddAttribute("style", styles);
					writer.AddAttribute("class", classes);
				}
				writer.RenderBeginTag("div");
				writer.Indent++;

				// input
				var inputId = adaptedControl.ClientID + "_" + itemCounter++;
				var uniqueId = this.Control.UniqueID; // +"$" + itemCounter++;
				writer.AddAttribute("type", "radio");
				writer.AddAttribute("value", item.Value);
				writer.AddAttribute("name", uniqueId);
				writer.AddAttribute("data-name", adaptedControl.ClientID);
				writer.AddAttribute("id", inputId);
				writer.AddAttribute("data-rootid", rootId);
				writer.AddAttribute("class", adaptedControl.CssClass);
				writer.AddAttribute("class", adaptedControl.CssClass);
				if (item.Selected)
				{
					writer.AddAttribute("checked", "checked");
				}

				writer.RenderBeginTag("input");
				writer.RenderEndTag();

				// label
				writer.AddAttribute("for", inputId);
				writer.AddAttribute("style", "display:inline-block;margin-left:3px;");
				writer.AddAttribute("class", adaptedControl.LabelCssClass);
				writer.RenderBeginTag("label");
				writer.Write(item.Text);
				writer.RenderEndTag();

				// div
				writer.Indent--;
				writer.RenderEndTag();

				if (Page != null)
				{
					Page.ClientScript.RegisterForEventValidation(uniqueId, item.Value);
				}
			}
			writer.Indent--;
		}
	}

	public class C5CheckBoxList : CheckBoxList
	{
		public C5CheckBoxList()
		{
			this.LabelCssClass = String.Empty;
		}
		public string LabelCssClass { get; set; }
	}

	public class C5CheckBoxListAdapter : WebControlAdapter
	{
		private List<string> reservedAttributes = new List<string>()
		{
			"class",
			"data-type",
			"name",
			"id"
		};

		protected override void RenderBeginTag(HtmlTextWriter writer)
		{
			var adaptedControl = (C5CheckBoxList)Control;
			writer.AddAttribute("class", "C5SLLC_chklistsub");
			writer.AddAttribute("data-type", "checkboxlist");
			writer.AddAttribute("name", adaptedControl.ClientID + "div");
			writer.AddAttribute("id", adaptedControl.ClientID);

			foreach (string key in adaptedControl.Attributes.Keys)
			{
				if (!reservedAttributes.Contains(key.ToLower()))
				{
					writer.AddAttribute(key, adaptedControl.Attributes[key]);
				}
			}

			writer.RenderBeginTag("div");
		}

		protected override void RenderEndTag(HtmlTextWriter writer)
		{
			writer.RenderEndTag();
		}

		protected override void EndRender(HtmlTextWriter writer)
		{

			if (Page != null)
			{
				Page.ClientScript.RegisterForEventValidation(Control.UniqueID);
			}
			base.EndRender(writer);
		}

		protected override void RenderContents(HtmlTextWriter writer)
		{
			var adaptedControl = (C5CheckBoxList)Control;
			int itemCounter = 0;
			string styles = String.Empty;
			string classes = String.Empty;
			string rootId = String.Empty;

			if (!String.IsNullOrWhiteSpace(adaptedControl.Attributes["data-id"]))
			{
				rootId = adaptedControl.Attributes["data-id"];
			}

			writer.Indent++;
			foreach (ListItem item in adaptedControl.Items)
			{
				// item div
				//writer.AddAttribute("data-type", "checkboxlist");
				//writer.AddAttribute("data-id", adaptedControl.Attributes["data-id"]);
				//writer.AddAttribute("name", adaptedControl.ClientID+"div");
				if (adaptedControl.RepeatDirection == RepeatDirection.Horizontal)
				{
					if (itemCounter > 0 && itemCounter % adaptedControl.RepeatColumns == 0)
					{
						styles = String.Format("width:{0}px;", adaptedControl.Width.Value);
						classes = " C5SLLC_chklstitemend";
					}
					else
					{
						styles = String.Format("width:{0}px;", adaptedControl.Width.Value);
						classes = " C5SLLC_chklstitem";
					}
					writer.AddAttribute("style", styles);
					writer.AddAttribute("class", classes);
				}
				writer.RenderBeginTag("div");
				writer.Indent++;

				// input
				
				var inputId = adaptedControl.ClientID + "_" + itemCounter;
				var uniqueId = this.Control.UniqueID + "$" + itemCounter++;
				writer.AddAttribute("type", "checkbox");
				writer.AddAttribute("value", item.Value);
				writer.AddAttribute("name", uniqueId);
				writer.AddAttribute("data-name", adaptedControl.ClientID);
				writer.AddAttribute("id", inputId);
				writer.AddAttribute("data-rootid", rootId);
				writer.AddAttribute("class", adaptedControl.CssClass);
				if (item.Selected)
				{
					writer.AddAttribute("checked", "checked");
				}

				writer.RenderBeginTag("input");
				writer.RenderEndTag();

				// label
				writer.AddAttribute("for", inputId);
				writer.AddAttribute("style", "display:inline-block;margin-left:3px;");
				writer.AddAttribute("class", adaptedControl.LabelCssClass);
				writer.RenderBeginTag("label");
				writer.Write(item.Text);
				writer.RenderEndTag();

				// div
				writer.Indent--;
				writer.RenderEndTag();

				if (Page != null)
				{
					Page.ClientScript.RegisterForEventValidation(uniqueId, item.Value);
				}

			}
			writer.Indent--;
		}
	}

	public class C5CheckBox : CheckBox
	{
		public C5CheckBox()
		{
			this.Value = String.Empty;
			this.LabelCssClass = String.Empty;
		}

		public string Value { get; set; }
		public string LabelCssClass { get; set; }
	}

	public class C5CheckBoxAdapter : WebControlAdapter
	{
		private List<string> reservedAttributes = new List<string>()
		{
			"checked",
			"style",
			"type",
			"value",
			"name",
			"id",
			"data-id"
		};

		protected override void RenderBeginTag(HtmlTextWriter writer)
		{
			writer.RenderBeginTag("div");
		}

		protected override void RenderEndTag(HtmlTextWriter writer)
		{
			writer.RenderEndTag();
		}

		protected override void EndRender(HtmlTextWriter writer)
		{
			if (Page != null)
			{
				Page.ClientScript.RegisterForEventValidation(Control.UniqueID);
			}
			base.EndRender(writer);
		}

		protected override void RenderContents(HtmlTextWriter writer)
		{
			var adaptedControl = (C5CheckBox)Control;
			string styles = String.Empty;
			string classes = String.Empty;
			string dataId = String.Empty;

			if (!String.IsNullOrWhiteSpace(adaptedControl.Attributes["data-id"]))
			{
				dataId = adaptedControl.Attributes["data-id"];
			}

			writer.Indent++;

			// item div
			if (!adaptedControl.Width.IsEmpty)
			{
				styles = String.Format("width:{0}px;", adaptedControl.Width.Value);
			}

			if (!String.IsNullOrWhiteSpace(styles))
			{
				writer.AddAttribute("style", styles);
			}

			classes = " C5SLLC_checkbox";
			if (!String.IsNullOrWhiteSpace(classes))
			{
				writer.AddAttribute("class", classes);
			}
			
			writer.RenderBeginTag("div");
			writer.Indent++;

			// input
			var inputId = adaptedControl.ClientID;
			var uniqueId = this.Control.UniqueID;

			writer.AddAttribute("type", "checkbox");
			writer.AddAttribute("value", adaptedControl.Value);
			writer.AddAttribute("name", uniqueId);
			writer.AddAttribute("id", inputId);
			writer.AddAttribute("data-id", dataId);
			if (!String.IsNullOrWhiteSpace(adaptedControl.CssClass))
			{
				writer.AddAttribute("class", adaptedControl.CssClass);
			}
			if (adaptedControl.Checked)
			{
				writer.AddAttribute("checked", "checked");
			}

			foreach (string key in adaptedControl.Attributes.Keys)
			{
				if (!reservedAttributes.Contains(key.ToLower()))
				{
					writer.AddAttribute(key, adaptedControl.Attributes[key]);
				}
			}

			writer.RenderBeginTag("input");
			writer.RenderEndTag();

			// label
			writer.AddAttribute("for", inputId);
			writer.AddAttribute("style", "display:inline-block;margin-left:3px;");
			writer.AddAttribute("class", adaptedControl.LabelCssClass);
			writer.RenderBeginTag("label");
			writer.Write(adaptedControl.Text);
			writer.RenderEndTag();

			// div
			writer.Indent--;
			writer.RenderEndTag();

			if (Page != null)
			{
				//Page.ClientScript.RegisterForEventValidation(uniqueId);
			}
		}
	}

	public class C5DatePicker : Telerik.Web.UI.RadDatePicker
	{
		protected override void Render(HtmlTextWriter writer)
		{
			foreach (string key in this.Attributes.Keys)
			{
				if (key == "placeholder")
				{
					this.DateInput.Attributes.Add(key, this.Attributes[key]);
				}
			}
			base.Render(writer);
		}
	}
}